# 编写forms表单类
# 注册用户 username password
from django import forms
from django.core.validators import EmailValidator,RegexValidator,MinLengthValidator
from django.core.exceptions import ValidationError

# 函数方法
def checkusername(values):
    """
    校验  账号中不能包含敏感字
    :param values: 获取到的数据
    :return:抛出异常
    """
    data_list = ["sb","zz"]
    for one in data_list:
        if one in values:
            # 不通过
            raise ValidationError("账号中不能包含敏感词")

# 类方式
class CheckUsername():
    def __call__(self, value):
        data_list = ["sb", "zz"]
        for one in data_list:
            if one in value:
                # 不通过
                raise ValidationError("账号中不能包含敏感词")

class PersonForm(forms.Form):
    username = forms.CharField(
        validators=[
            EmailValidator(message="必须为邮箱格式"),
            # checkusername,
            CheckUsername()
        ],
        error_messages={"required":"必填"}
    )
    password = forms.CharField(
        # max_length=10,
        # min_length=6,
        # required = True,
        validators=[
            RegexValidator("^(?=.*?[a-z])(?=.*>[A-Z])(?=.*?[0-9])[a-zA_Z0-9]{6,10}$",message="密码为6到16位数字字母组成")
        ]
    )

class UserInfoForm(forms.Form):
    nick_name = forms.CharField(
        error_messages={"required":"必填"}
    )
    phone = forms.CharField(
        validators=[
            RegexValidator("^1[356789]\d{9}$",message="格式不正确")
        ],
        error_messages={"required": "必填"}
    )
    email = forms.CharField(
        validators=[
            EmailValidator(message="必须为邮箱格式")
        ]
    )
    address = forms.CharField(
        error_messages={"required": "必填"}
    )

class RegisterForm(forms.Form):
    username = forms.CharField(
        validators=[
            MinLengthValidator(1,message="必填")
        ],
        error_messages={"required":"必填"}
    )














